package com.example.library_common.album.ui

import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.library_common.R
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.bean.AlbumFile
import com.example.library_common.album.bean.FunctionBean
import com.example.library_common.album.utils.ALBUM_SELECT_REFRESH
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.utils.even.MessageWrap
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class AlbumCapturedListFragment : SimpleListFragment<AlbumFile>() {

    companion object {
        @JvmStatic
        fun newInstance(): AlbumCapturedListFragment {
            val args = Bundle()
            val fragment = AlbumCapturedListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var funBean: FunctionBean? = null

    fun setFunBean(funBean: FunctionBean?){
        this.funBean=funBean
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        isAlwaysRefresh = false
        isEnableLoadMore = false
        isInitialRefresh = false
    }

    override fun getItemLayout() = R.layout.fragment_album_captured_item_layout

    override fun convertItem(helper: JssBaseViewHolder?, item: AlbumFile?) {
        helper?.setImageLocalUrl(R.id.albumCaptured,item?.path,R.drawable.img_erros_bg_rect)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .endSubType().build()
    }

    override fun addItemDecoration(): RecyclerView.ItemDecoration {
        val decoration =
            DividerItemDecoration(_mActivity, LinearLayoutManager.HORIZONTAL)
        decoration.setDrawable(
            ContextCompat.getDrawable(_mActivity, R.drawable.preview_list_shap) ?: ColorDrawable()
        )
        return decoration
    }

    override fun getLayoutManager(): RecyclerView.LayoutManager {
        return LinearLayoutManager(_mActivity,LinearLayoutManager.HORIZONTAL,false)
    }

    override fun netRequest() {

    }

    fun addAlbumFile(imageFile: AlbumFile) {
         mAdapter.addData(imageFile)
    }

    override fun handMessage(msg: MessageWrap?) {
//        super.handMessage(msg)
        if(msg?.message == ALBUM_SELECT_REFRESH){
            val checkedFiles = funBean?.checkedFiles
            mAdapter.clears()
            checkedFiles?.forEach {
                addAlbumFile(it)
            }
        }
    }
}